package cn.gcyl.module.biz.dal.mysql.platforminformation;

import cn.gcyl.framework.common.pojo.PageResult;
import cn.gcyl.framework.mybatis.core.mapper.BaseMapperX;
import cn.gcyl.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.gcyl.framework.security.core.LoginUser;
import cn.gcyl.module.biz.controller.admin.platforminformation.vo.*;
import cn.gcyl.module.biz.dal.dataobject.platforminformation.PlatformInformationDO;
import cn.gcyl.module.biz.dal.dataobject.thirdpartyinformation.ThirdPartyInformationDO;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

import static cn.gcyl.framework.security.core.util.SecurityFrameworkUtils.getLoginUser;
import static cn.gcyl.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

/**
 * 平台信息 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface PlatformInformationMapper extends BaseMapperX<PlatformInformationDO> {

    default PageResult<PlatformInformationDO> selectPage(PlatformInformationPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<PlatformInformationDO>()
                .eqIfPresent(PlatformInformationDO::getId, reqVO.getId())
                .eqIfPresent(PlatformInformationDO::getTitleType, reqVO.getTitleType())
                .eqIfPresent(PlatformInformationDO::getTitle, reqVO.getTitle())
                .eqIfPresent(PlatformInformationDO::getPurchasingCompany, reqVO.getPurchasingCompany())
                .eqIfPresent(PlatformInformationDO::getSort, reqVO.getSort())
                .betweenIfPresent(PlatformInformationDO::getVisitors, reqVO.getVisitors())
                .betweenIfPresent(PlatformInformationDO::getCollectors, reqVO.getCollectors())
                .betweenIfPresent(PlatformInformationDO::getShares, reqVO.getShares())
                .betweenIfPresent(PlatformInformationDO::getInquiries, reqVO.getInquiries())
                .eqIfPresent(PlatformInformationDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(PlatformInformationDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(PlatformInformationDO::getId));
    }
    default PageResult<PlatformInformationDO> getAppPlatformInformationPage(AppPlatformInformationPageReqVO reqVO) {
        LambdaQueryWrapperX<PlatformInformationDO> wrapperX = new LambdaQueryWrapperX<>();
                wrapperX.eqIfPresent(PlatformInformationDO::getPurchasingCompany, reqVO.getPurchasingCompany());
                wrapperX.eqIfPresent(PlatformInformationDO::getTitleType, reqVO.getTitleType());
                wrapperX.likeIfPresent(PlatformInformationDO::getTitle, reqVO.getTitle());
                wrapperX.likeIfPresent(PlatformInformationDO::getArea, reqVO.getArea());
                wrapperX.likeIfPresent(PlatformInformationDO::getTitle, reqVO.getColumns());

        // 支持标题的模糊匹配（LIKE）
        if (reqVO.getSubscriptionWord() != null && reqVO.getSubscriptionWord().length > 0) {
            wrapperX.and(wrapper -> {
                for (String title : reqVO.getSubscriptionWord()) {
                    wrapper.or().like(PlatformInformationDO::getTitle, title);
                }
            });
        }
                wrapperX.orderByAsc(PlatformInformationDO::getSort);
        return selectPage(reqVO, wrapperX);
    }

    /**
     * 根据ID和用户ID查询平台信息（检查用户是否在咨询用户列表中）
     */
    default PlatformInformationDO selectByIdAndUserId(Long id, Long userId) {
        return selectOne(new LambdaQueryWrapperX<PlatformInformationDO>()
                .eq(PlatformInformationDO::getId, id)
                .apply("FIND_IN_SET({0}, user_collect_ids)", userId));
    }


    default PageResult<PlatformInformationDO> getAppPlatformInformationList(AppPlatformInformationListReqVO reqVO) {
        Long userId = getLoginUserId();
        LambdaQueryWrapperX<PlatformInformationDO> queryWrapper = new LambdaQueryWrapperX<PlatformInformationDO>()
                .eqIfPresent(PlatformInformationDO::getTitleType, reqVO.getTitleType())
                .likeIfPresent(PlatformInformationDO::getArea, reqVO.getArea());

        // 支持标题的模糊匹配（LIKE）
        if (reqVO.getTitle() != null && reqVO.getTitle().length > 0) {
            queryWrapper.and(wrapper -> {
                for (String title : reqVO.getTitle()) {
                    wrapper.or().like(PlatformInformationDO::getTitle, title);
                }
            });
        }
        // 添加根据用户ID查询咨询用户IDs的条件
        if (userId != null) {
            queryWrapper.apply("FIND_IN_SET({0}, user_collect_ids)", userId);
        }
        queryWrapper.orderByAsc(PlatformInformationDO::getSort);

        // 使用分页查询
        return selectPage(reqVO, queryWrapper);
    }
}